package com.example.jdbc.dao.impl;

import com.example.jdbc.dao.UserDao;
import com.example.jdbc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 创建持久层对象的注解
 */
@Repository
public class UserDaoImpl implements UserDao {

    /**
     * 基于spring-boot-starter-jdbc 进行数据库操作
     * 前提是正确的配置了数据源dataSource，
     * 之后Spring boot 就会根据dataSource去初始化JdbcTemplate
     * 然后JdbcTemplate会被放到Ioc容器中
     */
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public int insertIntoUser(User user) {
        String insertSql = "insert into user(user_id,user_name,password,money)" +
                " values(default,?,?,?)";
        /**
         * JdbcTemplate 中的 update()可以执行所有DML操作
         */
        this.jdbcTemplate.update(insertSql,
                user.getUserName(),
                user.getPassword(),
                user.getMoney());
        return 0;
    }

    @Override
    public List<User> selectAllUsers() {
        String sql = "select * from user";
        /**
         * 用于返回多个查询结果
         */
        RowMapper<User> rowMapper = new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                user.setMoney(rs.getFloat("money"));
                return user;
            }
        };

        List<User> query = this.jdbcTemplate.query(sql, rowMapper);
        return query;
    }

    @Override
    public User selectUserByUserId(int userId) {

        String sql = "select * from user where user_id = ?";

        User user = new User();
        /**
         * 用于返回单个查询结果
         */
        RowCallbackHandler rowCallbackHandler = new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                user.setUserId(rs.getInt("user_id"));
                user.setUserName(rs.getString("user_name"));
                user.setPassword(rs.getString("password"));
                user.setMoney(rs.getFloat("money"));
            }
        };
        /**
         * 传递Sql中的参数
         */
        Object[] parameterArray = {userId};
        this.jdbcTemplate.query(sql, parameterArray, rowCallbackHandler);
        return user;
    }

    @Override
    public void updateUserByUserId(User user) {
        String sql = "update user set user_name = ? , password =? , money = ? " +
                " where user_id = ?";
        Object[] parameters = {
                user.getUserName(),
                user.getPassword(),
                user.getMoney(),
                user.getUserId()
        };
        /**
         * 不要忘记将参数传入
         */
        this.jdbcTemplate.update(sql, parameters);
    }

    @Override
    public void deleteUserById(int userId) {
        String sql = "delete from user where user_id = ?";
        /**
         * 传入的是..范型不是数组
         */
        this.jdbcTemplate.update(sql, userId);
    }
}
